<!DOCTYPE HTML>
<html>
	<head>
		<title>proton.js-game-fireworks</title>
		<meta name="viewport" id="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
		<meta charset="utf-8">
		<style type="text/css">
			body {
				font-family: Monospace;
				background-color: #f0f0f0;
				margin: 0px;
				overflow: hidden;
			}

			#testCanvas {
				background: #000000;
			}
		</style>
	</head>
	<body>
		<canvas id="testCanvas"></canvas>
		<script src="../../lib/requestAnimationFrame.min.js"></script>
    <script src="../../lib/stats.min.js"></script>
		<script src="//localhost:3001/build/proton.js"></script>
		<script>
			var canvas;
			var context;
			var proton;
			var renderer;
			var emitter;
			var stats;
			
			main();
			function main() {
				canvas = document.getElementById("testCanvas");
				canvas.width = window.innerWidth;
				canvas.height = window.innerHeight;
				context = canvas.getContext('2d');
				//context.globalCompositeOperation = "lighter";
				addStats();

				createProton();
				tick();
			}

			function addStats() {
				stats = new Stats();
				stats.setMode(2);
				stats.domElement.style.position = 'absolute';
				stats.domElement.style.left = '0px';
				stats.domElement.style.top = '0px';
				document.body.appendChild(stats.domElement);
			}

			function createProton(image) {
				proton = new Proton;
				emitter = new Proton.Emitter();
				emitter.rate = new Proton.Rate(new Proton.Span(1, 3), 1);
				
				emitter.addInitialize(new Proton.Mass(1));
				emitter.addInitialize(new Proton.Radius(2, 4));
				emitter.addInitialize(new Proton.P(new Proton.LineZone(10, canvas.height, canvas.width - 10, canvas.height)));
				emitter.addInitialize(new Proton.Life(1, 1.5));
				emitter.addInitialize(new Proton.V(new Proton.Span(4, 6), new Proton.Span(0, 0, true), 'polar'));
				
				emitter.addBehaviour(new Proton.Gravity(1));
				emitter.addBehaviour(new Proton.Color('#ff0000', 'random'));
				emitter.emit();
				proton.addEmitter(emitter);

				renderer = new Proton.CanvasRenderer(canvas);
				renderer.onProtonUpdate = function() {
					context.fillStyle = "rgba(0, 0, 0, 0.1)";
					context.fillRect(0, 0, canvas.width, canvas.height);
				};
				proton.addRenderer(renderer);

				////NOTICE :you can only use two emitters do this effect.In this demo I use more emitters want to test the emtter's life
				proton.addEventListener(Proton.PARTICLE_DEAD, function(particle) {
					if (Math.random() < .7)
						createFirstEmitter(particle);
					else
						createSecondEmitter(particle);
				});
			}

			function createFirstEmitter(particle) {
				var subemitter = new Proton.Emitter();
				subemitter.rate = new Proton.Rate(new Proton.Span(250, 300), 1);
				subemitter.addInitialize(new Proton.Mass(1));
				subemitter.addInitialize(new Proton.Radius(1, 2));
				subemitter.addInitialize(new Proton.Life(1, 3));
				subemitter.addInitialize(new Proton.V(new Proton.Span(2, 4), new Proton.Span(0, 360), 'polar'));
				
				subemitter.addBehaviour(new Proton.RandomDrift(10, 10, .05));
				subemitter.addBehaviour(new Proton.Alpha(1, 0));
				subemitter.addBehaviour(new Proton.Gravity(3));
				var color = Math.random() > .3 ? Proton.MathUtils.randomColor() : 'random';
				subemitter.addBehaviour(new Proton.Color(color));
				
				subemitter.p.x = particle.p.x;
				subemitter.p.y = particle.p.y;
				subemitter.emit('once', true);
				proton.addEmitter(subemitter);
			}

			function createSecondEmitter(particle) {
				var subemitter = new Proton.Emitter();
				subemitter.rate = new Proton.Rate(new Proton.Span(100, 120), 1);
				
				subemitter.addInitialize(new Proton.Mass(1));
				subemitter.addInitialize(new Proton.Radius(4, 8));
				subemitter.addInitialize(new Proton.Life(1, 2));
				subemitter.addInitialize(new Proton.V([1, 2], new Proton.Span(0, 360), 'polar'));
				
				subemitter.addBehaviour(new Proton.Alpha(1, 0));
				subemitter.addBehaviour(new Proton.Scale(1, .1));
				subemitter.addBehaviour(new Proton.Gravity(1));
				var color = Proton.MathUtils.randomColor();
				subemitter.addBehaviour(new Proton.Color(color));
				
				subemitter.p.x = particle.p.x;
				subemitter.p.y = particle.p.y;
				subemitter.emit('once', true);
				proton.addEmitter(subemitter);
			}

			function tick() {
				requestAnimationFrame(tick);

				stats.begin();
				proton.update();
				stats.end();
			}
		</script>
	</body>
</html>
